/*
 * @Description: 无
 * @version: 1.0.0
 * @Company: sdbean
 * @Author: hammercui
 * @Date: 2019-11-04 10:12:59
 * @LastEditors: hammercui
 * @LastEditTime: 2019-11-04 10:18:19
 */
package logger

import (
	"log"
	"os"

	"github.com/astaxie/beego/logs"
)

//设置缓存
var logger = logs.NewLogger(10000)

//获得日志文件名，按日区分
func logFileName() string {
	//timenow := time.Now();
	//return fmt.Sprintf("logger/ginstarter%d-%d-%d.log",timenow.Year(),timenow.Month(),timenow.Day())
	return "logger/web.log"
}

//手动初始化
func SetInit(debug bool) {
	fileName := logFileName()
	//文件配置,默认输出Info及以上
	fileConfig := `{"filename":"` + fileName + `","maxdays":356,"level":6,"separate":["error"]}`
	//console配置,默认输出Info及以上
	consoleConfig := `{"level":6}`
	if debug {
		fileConfig = `{"filename":"` + fileName + `","maxdays":356,"level":7,"separate":["error"]}`
		consoleConfig = `{"level":7}`
	}
	//配置输出文件
	err := logger.SetLogger(logs.AdapterMultiFile, fileConfig)
	if err != nil {
		log.Println("ERR", "init logger file err~", err)
		os.Exit(0)
	}
	//配置输出console
	//if(runtime.GOOS == "windows"){
	err1 := logger.SetLogger(logs.AdapterConsole, consoleConfig)
	if err1 != nil {
		log.Println("ERR", "init logger console err~", err1)
		os.Exit(0)
	}
	//}
	//设置异步
	logger.Async()
	//输出文件名和行号
	logger.EnableFuncCallDepth(true)

	logger.Info("logger init success!save path:%s", fileName)

	logger.SetLogFuncCallDepth(3)
}

func Trace(format string, v ...interface{}) {
	logger.Trace(format, v...)
}
func Debug(format string, v ...interface{}) {
	logger.Debug(format, v...)
}
func Info(format string, v ...interface{}) {
	logger.Info(format, v...)
}
func Warn(format string, v ...interface{}) {
	logger.Warn(format, v...)
}
func Error(format string, v ...interface{}) {
	logger.Error(format, v...)
}
func Critical(format string, v ...interface{}) {
	logger.Critical(format, v...)
}
